import { Component, OnInit, ViewChild } from '@angular/core';
import { ApiService } from '../../../core/api.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { DomSanitizer } from '@angular/platform-browser';
import { TableRowMergeComponent } from '../../../core/tableRowMerge.component';
import { MaintenanceComponent } from '../../../layout/components/maintenance.component';
import { LocalStorage } from '@core/local.storage';

@Component({
    selector: 'app-yearworkshop',
    templateUrl: './yearworkshop.component.html',
    styleUrls: ['./yearworkshop.component.less']
})
export class YearworkshopComponent implements OnInit {
    @ViewChild('mtecom', { static: true }) mtecom: MaintenanceComponent;
    tabsetIndex = 0;
    allChecked = false;
    indeterminate = false;
    displayData = [];
    data: any = [];
    yearpageValue = '50';
    yearselectedValue = '50';
    step1: any = true;
    rowTotalTable1 = 0;
    pageIndexTable1 = 1;
    rowpageTable1 = 1;
    pageTotal1 = '';

    allChecked2 = false;
    indeterminate2 = false;
    displayData2 = [];
    data2: any = [];
    listpageValue = '50';
    listselectedValue = '50';
    step2: any = false;
    rowTotalTable2 = 0;
    pageIndexTable2 = 1;
    rowpageTable2 = 1;
    pageTotal2 = '';

    addYearVisible = false;
    operateYearVisible = false;
    operateyearData = { 'type': '', 'title': '', 'data': [] };
    statusYearVisible = false;
    statusYearNid = '';
    addWorkVisible = false;
    addWorkButton = false;
    vcStatus = '';

    getyearlist: any;
    devinfolist: any;
    tempEquipmentValue: any = [];
    tempEquipmentIds: any = [];
    tempEquipmentGroup: any = [];
    tempstepyear = '';
    tempsteporgid = '';
    judgmentStatus: any = false;
    showstep3 = 'none';
    linetype = '';
    tabsTypevcCj = '';

    placeValue = '0';
    userInfo = JSON.parse(this.ls.get('loginInfo'));
    gqList: any = [];
    workareaid: any = '';
    lockHeadScroll = { y: window.innerHeight - 350 + 'px' };

    constructor(
        private apiService: ApiService,
        private message: NzMessageService,
        private modalService: NzModalService,
        private tabmercom: TableRowMergeComponent,
        private sanitizer: DomSanitizer,
        public ls: LocalStorage,
    ) { }

    ngOnInit() {
        this.reload();
        this.getGq();
    }
    reload() {
        this.readyload(this.rowpageTable1, this.yearpageValue);
    }
    tabsetChange(e) {
        this.tabsetIndex = e;
        switch (this.tabsetIndex) {
            case 0:// 车间
                this.tabsTypevcCj = '1';
                break;
            case 1:// 工区
                this.tabsTypevcCj = '0';
                break;
        }
        this.backstep();
        this.reload();
    }
    // 第一页列表数据
    readyload(page, rows) {
        switch (this.tabsetIndex) {
            case 0:// 车间
                this.tabsTypevcCj = '1';
                break;
            case 1:// 工区
                this.tabsTypevcCj = '0';
                break;
        }
        this.data = [];
        this.pageIndexTable1 = page;
        this.apiService.workshopExamine(page, rows, this.tabsTypevcCj).subscribe((res: any) => {
            if (res.type === 'success') {
                this.data = res.data.pageList;
                if (this.data.length) {
                    this.pageTotal1 = res.data.pageCount;
                    this.rowTotalTable1 = res.data.pageCount;
                    for (const item of this.data) {
                        switch (this.tabsetIndex) {
                            case 0:// 车间
                                switch (item.VC_STATUS) {
                                    case '0':
                                    case '3':
                                        item.disabled = false;
                                        break;
                                    default:
                                        item.disabled = true;
                                }
                                break;
                            case 1:// 工区
                                switch (item.VC_STATUS) {
                                    case '1':
                                        item.disabled = false;
                                        break;
                                    default:
                                        item.disabled = true;
                                }
                                break;
                        }
                    }
                }
            }
        });
    }
    // 第二页列表数据
    startstep(page, N_YEAR, orgid, row, filter, linetype, N_ID, VC_STATUS) {
        this.statusYearNid = N_ID;
        this.linetype = linetype;
        this.rowpageTable2 = page;
        this.pageIndexTable2 = page;
        this.tempstepyear = N_YEAR;
        this.tempsteporgid = orgid;
        this.vcStatus = VC_STATUS;
        // 控制显示新增作业按钮
        switch (this.tabsetIndex) {
            case 0:
                switch (this.vcStatus) {
                    case '0':
                    case '3':
                        this.addWorkButton = true;
                        break;
                }
                break;
            case 1:
                switch (this.vcStatus) {
                    case '1':
                        this.addWorkButton = true;
                        break;
                }
                break;
        }
        this.data2 = [];
        this.apiService.workshopApprove(page, N_YEAR, orgid, row, filter, linetype, this.tabsTypevcCj).subscribe((res: any) => {
            if (res.type === 'success') {
                this.data2 = res.data.pageList;
                if (this.data2.length) {
                    this.pageTotal2 = res.data.pageCount;
                    this.rowTotalTable2 = res.data.pageCount;

                    for (const item of this.data2) {
                        // 处理换行
                        if (item.VC_NOTE && item.VC_NOTE !== 'null') {
                            item.REVC_NOTE = this.sanitizer.bypassSecurityTrustHtml(item.VC_NOTE.replace(/\[br\]/g, '<br>').replace(/\[nbsp\]/g, ' '));
                        } else {
                            item.REVC_NOTE = ' ';
                        }
                        if (item.VC_STATUS == '1' || item.VC_STATUS == '5') {
                            item.disabled = false;
                            this.judgmentStatus = true;
                        } else {
                            item.disabled = true;
                        }
                    }
                    // 表格合并
                    this.tabmercom.mergeRowSet('VC_CLASS,VC_ITEM$NAME,VC_RECYCLE$DESC,VC_UNIT,DEVSUMS,REVC_NOTE', this.data2, (tableData) => {
                        this.data2 = tableData;
                    });
                }
                this.step1 = false;
                this.step2 = true;
                this.showstep3 = 'none';
            }
        });
    }
    // 筛选数据
    yearFilterData(data) {
        let url = '';
        data.yearstatus ? url += '&yearstatus=' + data.yearstatus : url += '';
        data.protype ? url += '&protype=' + data.protype : url += '';
        data.level ? url += '&level=' + data.level : url += '';
        this.startstep(this.rowpageTable2, this.tempstepyear, this.tempsteporgid, this.listpageValue, url, this.linetype, this.statusYearNid, this.vcStatus);
    }
    // 返回第一页
    backstep() {
        this.step1 = true;
        this.step2 = false;
        this.showstep3 = 'none';
        this.pageIndexTable2 = 1;
        this.rowpageTable2 = 1;
        this.judgmentStatus = false;
        this.addWorkButton = false;
        this.reload();
    }
    // 返回第二页
    backstep2() {
        this.step1 = false;
        this.step2 = true;
        this.showstep3 = 'none';
        this.rowpageTable2 = 1;
        this.startstep(this.rowpageTable2, this.tempstepyear, this.tempsteporgid, this.listpageValue, '', this.linetype, this.statusYearNid, this.vcStatus);
    }
    mainBackPage() {
        this.startstep(this.rowpageTable2, this.tempstepyear, this.tempsteporgid, this.listpageValue, '', this.linetype, this.statusYearNid, this.vcStatus);
    }

    // 分页数量-第一页
    yearselectedChange() {
        this.yearpageValue = this.yearselectedValue;
        this.reload();
    }
    // 分页数量-第二页
    listselectedChange() {
        this.listpageValue = this.listselectedValue;
        this.startstep(this.rowpageTable2, this.tempstepyear, this.tempsteporgid, this.listpageValue, '', this.linetype, this.statusYearNid, this.vcStatus);
    }
    // 第三页详情数据
    maintain(index, itemid, nid, type) {
        this.mtecom.getData(this.data2, index, itemid, nid, type, (yearlist, devlist) => {
            this.getyearlist = yearlist;
            this.devinfolist = devlist;
        }, this.tempsteporgid);
        this.step1 = false;
        this.step2 = false;
        this.showstep3 = 'block';
    }
    // 全选
    checkAll(value: boolean): void {
        this.displayData.forEach(data => {
            if (!data.disabled) {
                data.checked = value;
            }
        });
        this.refreshStatus();
    }
    currentPageDataChange($event: Array<{ name: string; age: number; address: string; checked: boolean; disabled: boolean; }>): void {
        this.displayData = $event;
        this.refreshStatus();
    }
    refreshStatus(): void {
        const allChecked = this.displayData.filter(value => !value.disabled).every(value => value.checked == true);
        const allUnChecked = this.displayData.filter(value => !value.disabled).every(value => !value.checked);
        this.allChecked = allChecked;
        this.indeterminate = (!allChecked) && (!allUnChecked);
    }
    rowChangeTable1(ev) {
        this.rowpageTable1 = ev;
        this.reload();
    }
    checkAll2(value: boolean): void {
        this.displayData2.forEach(data => {
            if (!data.disabled) {
                data.checked = value;
            }
        });
        this.refreshStatus2();
    }
    currentPageDataChange2($event: Array<{ name: string; age: number; address: string; checked: boolean; disabled: boolean; }>): void {
        this.displayData2 = $event;
        this.refreshStatus2();
    }
    refreshStatus2(): void {
        const allChecked2 = this.displayData2.filter(value => !value.disabled).every(value => value.checked == true);
        const allUnChecked2 = this.displayData2.filter(value => !value.disabled).every(value => !value.checked);
        this.allChecked2 = allChecked2;
        this.indeterminate2 = (!allChecked2) && (!allUnChecked2);
    }
    rowChangeTable2(ev) {
        this.rowpageTable2 = ev;
        this.startstep(this.rowpageTable2, this.tempstepyear, this.tempsteporgid, this.listpageValue, '', this.linetype, this.statusYearNid, this.vcStatus);
    }

    // 显示[新编]弹出层
    yearShowModal() {
        this.addYearVisible = true;
    }
    yearHideModal() {
        this.addYearVisible = false;
    }
    // 显示[删除]弹出层
    operateShowModal(type, title) {
        this.operateyearData.type = type;
        this.operateyearData.title = title;
        this.operateyearData.data = this.displayData;
        this.operateYearVisible = true;
    }
    operateHideModal() {
        this.operateYearVisible = false;
    }
    // 显示[状态]弹出层
    statusShowModal(nid) {
        this.statusYearNid = nid;
        this.statusYearVisible = true;
    }
    statusHideModal() {
        this.statusYearVisible = false;
    }
    // 显示[作业项目]弹出层
    addWorkShowModal() {
        this.addWorkVisible = true;
    }
    addWorkHideModal() {
        this.addWorkVisible = false;
    }
    getGq() {
        if (!this.userInfo.VC_ORG$ID) {
            return;
        }
        this.apiService.getgongquByCjId(this.userInfo.VC_ORG$ID).subscribe(res => {
            this.gqList = res;
        });
    }
    deleteItem(id) {
        if (!id) {
            return;
        }
        const param = { nnDID: id };
        this.apiService.yearlyDataDelete(param).subscribe(res => {
            if (res.type === 'success') {
                this.message.success('删除成功');
                this.mainBackPage();
            } else {
                this.message.success('删除失败');
            }
        });
    }
}
